<?php
$album_type=0;
$page=0;
$music_limit=15;
if (isset($_GET['page'])) $page=intval($_GET['page']);

if (isset($_REQUEST['album_type']))
	$album_type=intval($_REQUEST['album_type']);

$start_at = $page  * $music_limit;
// get count all musics
$count_post =$db->countObjectsBySql("SELECT COUNT(website_musics.id) as c FROM website_listing  INNER JOIN website_musics  ON website_musics.album_id = website_listing.id WHERE website_musics.tongpho > 0 AND album_type={$album_type}");
// cal page_count
// page split
		// cal how many posts
			$page_count=floor($count_post / $music_limit);
			if ($count_post % $music_limit != 0)
			  $page_count++;
			// build page array
			$pages = array();
			for ($i=1;$i<=$page_count;$i++)
				$pages[$i-1] = $i;
$musics=$db->selectObjectsBySql("SELECT website_musics.* FROM website_listing  INNER JOIN website_musics  ON website_musics.album_id = website_listing.id WHERE website_musics.tongpho > 0 AND album_type={$album_type} ORDER BY id DESC LIMIT {$start_at},{$music_limit}");
for($j=0; $j<count($musics); $j++) {
	$album=$db->selectObject('listing','id ='.$musics[$j]->album_id);
	$musics[$j]->downloadable=1;
	$musics[$j]->singer_search = htmlspecialchars($musics[$j]->singer);
	$musics[$j]->high_quality_id=$musics[$j]->tongpho;
		$musics[$j]->album_name = $album->name;
				
				// get composer name
				$composer=$db->selectObject('listing',"list_type = 1 AND id = {$musics[$j]->composer_id}");
				$composer_name='';
				if ($composer)
				{
					$composer_name=$composer->name;
				}
				
				//  build tongpho file path
				$tp_file=$db->selectObject("file", "id = " . $musics[$j]->tongpho);
				$musics[$j]->tongpho="http://" . $_SERVER['HTTP_HOST']."/giangdong/" .$tp_file->directory . "/" . $tp_file->filename;
				
				// for tooltip
				$musics[$j]=musicsmodule::make_info($musics[$j], $composer_name, $album->name);
		}				
$template = new template('musicsmodule','_tongpho');			
$template->assign('musics',$musics);
$template->assign('page',$page);
$template->assign('page_count',$page_count);
$template->assign('pages',$pages);
$template->assign('album_type',$album_type);
$template->assign('album_show_require',1);
$template->assign('music_no_start',$start_at);
if ($album_type == 0)
{
		$template->assign('moduletitle','codoc');
}
else
{
	$template->assign('moduletitle','trutinh');
}

$template->output();

?>
